<template>
<div class="app-container">
  <div style="width: 800px">
    <el-form ref="form" :rules="rules" :model="form" label-width="120px">
      <el-form-item label="活动名称" prop="name">
        <el-input size="small" v-model="form.name" placeholder="请输入活动名称" />
      </el-form-item>
      <el-form-item label="活动描述" prop="des">
        <el-input size="small" v-model="form.des" placeholder="请输入活动描述" />
      </el-form-item>
      <el-form-item label="活动海报" required>
        <div style="color:#999">
          建议尺寸290*170px，JPG、PNG格式， 图片小于5M。
        </div>
        <el-upload
          :class="imgFilesList.length?'hiddenUpload':''"
          action="https://service.bairicun.com/file/upload"
          list-type="picture-card"
          :on-success="handleSuccess"
          :on-preview="handlePictureCardPreview"
          :file-list="imgFilesList"
          :on-remove="handleRemove">
          <i class="el-icon-plus"></i>
        </el-upload>
        <el-dialog :visible.sync="dialogVisible">
          <img width="100%" :src="dialogImageUrl" alt="">
        </el-dialog>
      </el-form-item>
      <el-form-item label="活动地点" prop="place">
        <el-input size="small" v-model="form.place" placeholder="请输入活动地点" />
      </el-form-item>
      <el-form-item label="活动时间" prop="activityTime">
        <el-date-picker
          style="width:100%;"
          size="small"
          v-model="form.activityTime"
          type="datetimerange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="yyyy-MM-dd HH:mm:ss"
          align="right">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="报名时间" prop="baoTime">
        <el-date-picker
          style="width:100%;"
          size="small"
          v-model="form.baoTime"
          type="datetimerange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="yyyy-MM-dd HH:mm:ss"
          align="right">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="活动详情" prop="detail">
        <tinymce v-model="form.detail" :height="300" placeholder="请输入活动详情"/>
      </el-form-item>
      <!-- <el-form-item label="所属课程">
        <span style="margin-right:20px;" v-if="selectName">已选择：{{selectName}}</span>
        <el-button size="small" type="primary" @click="openke">选择课程</el-button>
      </el-form-item> -->
      <el-form-item label="活动费用" prop="fee">
        <!-- <div style="color:#ff0000">
          注：如选择选择课程，按照课程费用计费
        </div> -->
        <el-input :disabled="Boolean(radio)" size="small" v-model="form.fee" placeholder="请输入费用" />
      </el-form-item>
      <el-form-item label="可提现打卡天数" prop="clockInDays">
        <el-input size="small" v-model="form.clockInDays" placeholder="请输入可提现打卡天数" />
      </el-form-item>
      <el-form-item label="上限名额" prop="limitNum">
        <el-input size="small" v-model="form.limitNum" placeholder="请输入上限名额" />
      </el-form-item>
      <el-form-item>
        <el-button size="small" type="primary" @click="onSubmit('form')">保存</el-button>
        <el-button size="small" @click="onCancel">取消</el-button>
      </el-form-item>
    </el-form>


    <el-dialog top="10vh" width="1000px" title="选择课程" :visible.sync="courseModal">
      <div style="display:flex;align-items: center;justify-content: space-between;margin:0 0 10px;">
        <div style="display:flex;align-items: center;">
          <el-form :inline="true" class="demo-form-inline">
            <el-form-item label="课程名称">
              <el-input clearable size="small" v-model="columnName" placeholder="课程名称"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button size="small" type="primary" @click="getCourseList">搜索</el-button>
            </el-form-item>
          </el-form>
        </div>
      </div>
      <el-table
        @current-change="handleCurrentChangeSelect"
        highlight-current-row
        row-key="id"
        style="width: 100%"
        height="400"
        ref="multipleTable"
        v-loading="listLoading"
        :data="courseList"
        element-loading-text="Loading"
        border
        fit
        empty-text="暂无数据"
      >
        <el-table-column label="ID" width="150">
          <template slot-scope="scope">{{ scope.row.id }}</template>
        </el-table-column>
        <el-table-column label="课程名称">
          <template slot-scope="scope">{{ scope.row.name }}</template>
        </el-table-column>
        <el-table-column label="主讲教师" width="200">
          <template slot-scope="scope">
            {{ scope.row.teacherList&&scope.row.teacherList.length?scope.row.teacherList[0].realName:'' }}
          </template>
        </el-table-column>
      </el-table>

      <div style="margin:10px 0 0 0;text-align:right;">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="itemObj.pageNum"
          :page-sizes="[10, 30, 50, 100]"
          :page-size="itemObj.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="itemObj.total"
        ></el-pagination>
      </div>

      <div slot="footer" class="dialog-footer" style="text-align:center;">
        <el-button size="small" type="primary" @click="courseModal = false">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</div>
</template>

<script>
import Tinymce from '@/components/Tinymce'
import {
  saveApi,
  getDetailApi
} from '@/api/activity'
import {
  getListApi,
} from '@/api/course'
export default {
  components: {Tinymce},
  data() {
    return {
      radio: '',
      tableList: [],
      listLoading: false,
      formInline: {
        title: ''
      },
      form: {
        id: '',
        name: '',
        des: '',
        place: '',
        activityTime: [],
        baoTime: [],
        detail: '',
        fee: '',
        limitNum: '',
        clockInDays: ''
      },
      rules: {
        name: [{
          required: true,
          message: '请输入标题',
          trigger: 'blur'
        }, ],
        clockInDays: [{
          required: true,
          message: '请输入提现打卡天数',
          trigger: 'blur'
        }, ],
        des: [{
          required: true,
          message: '请输入描述',
          trigger: 'blur'
        }, ],
        place: [{
          required: true,
          message: '请输入活动地点',
          trigger: 'blur'
        }, ],
        activityTime: [{
          required: true,
          message: '请选择活动时间',
          trigger: 'change'
        }, ],
        baoTime: [{
          required: true,
          message: '请选择报名时间',
          trigger: 'change'
        }, ],
      },
      dialogImageUrl: '',
      dialogVisible: false,
      uploadStatus: false,
      uploadUrl: '',
      uploadImgId: '',
      modalVisible: false,
      imgFilesList: [],
      courseModal: false,
      courseList: [],
      columnName: '',
      itemObj: {
        pageSize: 10,
        pageNum: 1,
        total: 0,
      },
      selectObj: {},
      selectName: '',
      selectId: '',
    }
  },
  mounted() {
    this.getDetail()
  },
  methods: {
    handleSizeChange(val) {
      this.itemObj.pageSize = val;
      this.getCourseList()
    },
    handleCurrentChange(val) {
      this.itemObj.pageNum = val;
      this.getCourseList()
    },
    handleCurrentChangeSelect(e) {
      this.selectObj = e;
      this.selectName = e.name;
      this.selectId = e.id;
    },
    setCurrent(row) {
      this.$refs.multipleTable.setCurrentRow(row);
    },
    openke() {
      console.log(1)
      this.courseModal = true
      this.itemObj.pageNum = 1
      this.itemObj.pageSize = 10
      this.courseList = []
      this.getCourseList()
    },
    getCourseList() {
      this.listLoading = true;
      getListApi({
        name: this.columnName,
        state: 1,
        pageSize: this.itemObj.pageSize,
        pageNum: this.itemObj.pageNum
      }).then(res => {
        this.listLoading = false;
        this.courseList = res.data.result;
        this.itemObj.total = res.data.total;
        this.courseList.forEach((item,index) => {
          if(item.id == this.form.courseId) {
            this.setCurrent(item)
          }
        })
      });
    },
    getDetail() {
      getDetailApi(this.$route.query.editId).then((response) => {
        this.form.id = response.data.id
        this.form.name = response.data.name
        this.form.des = response.data.des
        this.form.place = response.data.place
        this.form.fee = response.data.fee
        this.form.detail = response.data.detail
        this.form.limitNum = response.data.limitNum
        this.form.clockInDays = response.data.clockInDays
        this.form.activityTime = [response.data.startTime, response.data.endTime]
        this.form.baoTime = [response.data.enrollStartTime, response.data.enrollEndTime]
        this.imgFilesList = [
          {
            url: response.data.picUrl,
            id: response.data.picId,
          }
        ]
        this.uploadImgId = response.data.picId
        this.form.courseId = response.data.courseId
      })
    },
    chooseone(row){
      this.radio = row.id
    },
    handleRemove(file, fileList) {
      this.imgFilesList = []
      this.uploadUrl = ''
      this.uploadImgId = ''
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    handleSuccess(e) {
      if(e.code == 200) {
        this.uploadStatus = true
        this.uploadUrl = e.data.url
        this.uploadImgId = e.data.id
        this.imgFilesList = [
          {
            url: e.data.url,
            id: e.data.id,
          }
        ]
      }
    },
    onSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if(this.imgFilesList.length === 0) {
            this.$message.error('请上传活动海报！');
          } else {
            saveApi({
              id: this.form.id,
              name: this.form.name,
              des: this.form.des,
              place: this.form.place,
              fee: this.form.fee,
              limitNum: this.form.limitNum,
              detail: this.form.detail,
              startTime: this.form.activityTime[0],
              endTime: this.form.activityTime[1],
              enrollStartTime: this.form.baoTime[0],
              enrollEndTime: this.form.baoTime[1],
              picId: this.uploadImgId,
              courseId: this.form.courseId,
              clockInDays: this.form.clockInDays
            }).then((res) => {
              this.$message.success('操作成功！');
              setTimeout(() => {
                this.$router.back(-1)
              }, 1000);
            })
          }
        } else {
          return false;
        }
      });
    },
    onCancel() {
      this.$router.back(-1)
    },
  }
}
</script>
<style>
  .hiddenUpload .el-upload--picture-card{
    display:none !important;
  }
</style>
<style scoped>
.line {
  text-align: center;
}
</style>
